package com.mlethe.library.multi.demo;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mlethe.library.multi.api.MultiApi;
import com.mlethe.library.multi.api.MultiMedia;
import com.mlethe.library.multi.api.ShareAction;
import com.mlethe.library.multi.api.ShareScene;
import com.mlethe.library.multi.api.entity.ImageObject;
import com.mlethe.library.multi.api.entity.ShareImage;
import com.mlethe.library.multi.api.entity.ShareMini;
import com.mlethe.library.multi.api.entity.ShareMusic;
import com.mlethe.library.multi.api.entity.ShareText;
import com.mlethe.library.multi.api.entity.ShareVideo;
import com.mlethe.library.multi.api.entity.ShareWeb;
import com.mlethe.library.multi.api.listener.OnActionListener;
import com.mlethe.library.multi.demo.databinding.ActivityWechatShareBinding;

import java.io.File;
import java.util.Map;

/**
 * 微信分享
 */
public class WechatShareActivity extends AppCompatActivity {

    private ActivityWechatShareBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWechatShareBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        OnActionListener listener = new OnActionListener() {
            @Override
            public void onComplete(@NonNull MultiMedia media, @NonNull Map<String, String> map) {
                Log.e("multi", "onComplete: " + media + ", map->" + map);
                Toast.makeText(WechatShareActivity.this, media + "分享完成：" + map, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(@NonNull MultiMedia media) {
                Log.e("multi", "onCancel: " + media);
                Toast.makeText(WechatShareActivity.this, media + "分享取消", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(@NonNull MultiMedia media, int code, String msg) {
                Log.e("multi", "onFailure: " + media + "，code:" + code + ", msg: " + msg);
                Toast.makeText(WechatShareActivity.this, media + "分享失败，code:" + code + ", msg: " + msg, Toast.LENGTH_SHORT).show();
            }
        };
        binding.textBtn.setOnClickListener(v -> {
            ShareText share = new ShareText("分享测试内容");
            new ShareAction(MultiMedia.WECHAT)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
        binding.imgBtn.setOnClickListener(v -> {
            File externalFilesDir = getExternalFilesDir(null);
            ShareImage share = new ShareImage(new ImageObject(externalFilesDir + "/file_images/ccc.JPG"));
            new ShareAction(MultiMedia.WECHAT)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
        binding.webBtn.setOnClickListener(v -> {
            ShareWeb share = new ShareWeb("https://www.baidu.com");
            share.setTitle("分享网页");
            share.setDescription("分享网页测试描述");
            File externalCacheDir = getExternalCacheDir();
            share.setThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg"));
            new ShareAction(MultiMedia.WECHAT)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
        binding.videoBtn.setOnClickListener(v -> {
            File externalCacheDir = getExternalCacheDir();
            ShareVideo share = new ShareVideo("https://illoll.sdfsdf.cn/video/huai_auen.mp4");
            share.setTitle("分享视频");
            share.setDescription("分享视频描述");
            share.setThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg"));
            new ShareAction(MultiMedia.WECHAT)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
        binding.miniBtn.setOnClickListener(v -> {
            ShareMini share = new ShareMini("gh_d43f693ca31f", "/pages/media");
            share.setTitle("小程序消息Title");
            share.setDescription("小程序消息Desc");
            File externalCacheDir = getExternalCacheDir();
            share.setThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg"));
            new ShareAction(MultiMedia.WECHAT)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
        binding.musicBtn.setOnClickListener(v -> {
            File externalCacheDir = getExternalCacheDir();
            ShareMusic share = new ShareMusic("http://xxx/xx.mp3", "https://www.qq.com");
            share.setSingerName("xxx"); // 必填，歌手名
            share.setDuration(120000); // 必填，歌曲时长（毫秒）
            share.setSongLyric("xxx"); // 选填，歌词
            share.setAlbumName("album_xxx"); // 选填，音乐专辑名
            share.setMusicGenre("流行歌曲"); // 选填，音乐流派
            share.setIssueDate(1610713585); // 选填，发行时间Unix时间戳(秒)
            share.setIdentification("sample_identification"); // 选填，音乐标识符
            share.addThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg")); // 选填，专辑图本地文件路径
            share.setTitle("歌曲名称"); // 必填，不能为空
            share.setDescription("歌曲描述");  // 选填，建议与歌手名字段 singerName 保持一致
            share.setThumbImage(new ImageObject(externalCacheDir + "/file_images/eee.jpg"));
            new ShareAction(MultiMedia.WECHAT)
                    .setShare(share)
                    .setShareScene(getShareScene())
                    .setCallback(listener)
                    .share(this);
        });
    }

    /**
     * 获取分享的场景
     *
     * @return
     */
    private ShareScene getShareScene() {
        int id = binding.rgScene.getCheckedRadioButtonId();
        if (id == R.id.rb_friend) {
            return ShareScene.FRIEND;
        } else if (id == R.id.rb_circle) {
            return ShareScene.CIRCLE;
        } else {
            return ShareScene.FAVORITE;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MultiApi.getInstance().resume();
    }
}
